#include "api.h"

#define FRPC_INI_PATH "./frpc.ini"

static uint32_t msgid = 0;

static int mqtt_pub(mqtt_handle_t* handle,char* topic,char* payload,int qos)
{
	#define PUB_TIMEOUT     10000L
    MQTTClient client = handle->client;
	MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
	int rc;
    cfg_pu_t* pu = (cfg_pu_t*)handle->param;

    if(MQTTClient_isConnected(client) == 0)
    {
        return -1;
    }

    // protocol_t* protocol = (protocol_t*)pu->protocol.setting;
    // if(protocol->debug)
    {
        printf("Message send\n");
        printf("  Topic: %s\n",topic);
        printf("Payload: %s\n\n",payload);
    }

	pubmsg.payload = payload;
	pubmsg.payloadlen = (int)strlen(payload);
	pubmsg.qos = qos;
	pubmsg.retained = 0;
	if ((rc = MQTTClient_publishMessage(client, topic, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
	{
		//  UTIL_LOG_E(pu,"Failed to start sendMessage, return code %d", rc); 
		return -1;
	}

	if(MQTTClient_waitForCompletion(client, token, PUB_TIMEOUT) != MQTTCLIENT_SUCCESS)
    {
        return -1;
    }

    pu->pkg_send++;

    return 0;
}


int api_operate_rsp(mqtt_handle_t* handle,char *msgid,int timestamp)
{
    cfg_pu_t* pu = (cfg_pu_t*)handle->param;
    api_t* api = (api_t*)pu->hanlder.api;

    //  UTIL_LOG_I(pu,"opertate response...");

    cJSON* root = cJSON_CreateObject();
    cJSON_AddItemToObject(root,"msgid",cJSON_CreateString(msgid));
    cJSON_AddItemToObject(root,"method",cJSON_CreateString("operate"));
    cJSON_AddItemToObject(root,"sn",cJSON_CreateString("TEST0123456789"));
    cJSON_AddItemToObject(root,"timestamp",cJSON_CreateNumber(timestamp));
    cJSON_AddItemToObject(root,"res",cJSON_CreateNumber(1));

    char* payload = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);

    printf("operate frpc response: %s\n", payload);

    char topic[200] = "";
    sprintf(topic, "indicate/dev/ODQwODQ3MTgzNzA0Mjg5Mjgw/%s", "TEST0123456789");
    mqtt_pub(handle,topic,payload,0);
    free(payload);

    return 0;
}

int api_restart_rsp(mqtt_handle_t* handle,char *msgid,int timestamp)
{
    cfg_pu_t* pu = (cfg_pu_t*)handle->param;
    api_t* api = (api_t*)pu->hanlder.api;

    //  UTIL_LOG_I(pu,"restart response...");

    cJSON* root = cJSON_CreateObject();
    cJSON_AddItemToObject(root,"msgid",cJSON_CreateString(msgid));
    cJSON_AddItemToObject(root,"method",cJSON_CreateString("restart"));
    cJSON_AddItemToObject(root,"sn",cJSON_CreateString("TEST0123456789"));
    cJSON_AddItemToObject(root,"timestamp",cJSON_CreateNumber(timestamp));
    cJSON_AddItemToObject(root,"res",cJSON_CreateNumber(1));

    char* payload = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);

    char topic[200] = "";
    sprintf(topic, "sys/dev/ODQwODQ3MTgzNzA0Mjg5Mjgw/%s", "TEST0123456789");
    mqtt_pub(handle,topic,payload,0);
    free(payload);

    return 0;
}

int api_read_basic_rsp(mqtt_handle_t* handle,char *msgid,int timestamp)
{
    cfg_pu_t* pu = (cfg_pu_t*)handle->param;
    api_t* api = (api_t*)pu->hanlder.api;

    //  UTIL_LOG_I(pu,"read basic response...");

    cJSON* root = cJSON_CreateObject();
    cJSON_AddItemToObject(root,"msgid",cJSON_CreateString(msgid));
    cJSON_AddItemToObject(root,"method",cJSON_CreateString("info"));
    cJSON_AddItemToObject(root,"sn",cJSON_CreateString("TEST0123456789"));
    cJSON_AddItemToObject(root,"timestamp",cJSON_CreateNumber(timestamp));
    cJSON_AddItemToObject(root,"res",cJSON_CreateNumber(1));

    cJSON* cJSON_Payload = cJSON_CreateObject();
    cJSON_AddItemToObject(cJSON_Payload,"sn",cJSON_CreateString("TEST0123456789"));
    cJSON_AddItemToObject(cJSON_Payload,"devName",cJSON_CreateString(g_cfg->g_basic->gw_name));
    cJSON_AddItemToObject(cJSON_Payload,"projectName",cJSON_CreateString(g_cfg->g_basic->proj_name));
    cJSON_AddItemToObject(cJSON_Payload,"country",cJSON_CreateString("中国"));
    cJSON_AddItemToObject(cJSON_Payload,"location",cJSON_CreateString("江苏省江阴市"));

    char time[100] = "";
    util_hw_get_systime(time);
    cJSON_AddItemToObject(cJSON_Payload,"time",cJSON_CreateString(time));
    cJSON_AddItemToObject(cJSON_Payload,"timeZone",cJSON_CreateString("+08:00"));

    char buff_runtime[30] = "";
    sprintf(buff_runtime,"%ld",g_param.run_time);
    cJSON_AddItemToObject(cJSON_Payload,"runTime",cJSON_CreateString(buff_runtime));
    cJSON_AddItemToObject(cJSON_Payload,"runState",cJSON_CreateString("run"));
    cJSON_AddItemToObject(cJSON_Payload,"ethState",cJSON_CreateString("run"));
    cJSON_AddItemToObject(cJSON_Payload,"gsmState",cJSON_CreateString("run"));

    cJSON_AddItemToObject(cJSON_Payload,"gsmRssi",cJSON_CreateNumber(24));
    // cJSON_AddItemToObject(cJSON_Payload,"wifiRssi",cJSON_CreateNumber(90));
    cJSON_AddItemToObject(cJSON_Payload,"loraRssi",cJSON_CreateNumber(-20));

    cJSON_AddItemToObject(cJSON_Payload,"sendPkt",cJSON_CreateNumber(999));
    cJSON_AddItemToObject(cJSON_Payload,"recvPkt",cJSON_CreateNumber(999));

    cJSON_AddItemToObject(cJSON_Payload,"devCnt",cJSON_CreateNumber(10));
    cJSON_AddItemToObject(cJSON_Payload,"devOnlineCnt",cJSON_CreateNumber(10));

    cJSON_AddItemToObject(cJSON_Payload,"uartCnt",cJSON_CreateNumber(6));
    cJSON_AddItemToObject(cJSON_Payload,"ethCnt",cJSON_CreateNumber(1));
    cJSON_AddItemToObject(cJSON_Payload,"loraCnt",cJSON_CreateNumber(1));
    cJSON_AddItemToObject(cJSON_Payload,"wifiCnt",cJSON_CreateNumber(0));
    cJSON_AddItemToObject(cJSON_Payload,"gsmCnt",cJSON_CreateNumber(1));

    cJSON_AddItemToObject(cJSON_Payload,"diCnt",cJSON_CreateNumber(8));
    cJSON_AddItemToObject(cJSON_Payload,"doCnt",cJSON_CreateNumber(4));
    cJSON_AddItemToObject(cJSON_Payload,"aiCnt",cJSON_CreateNumber(8));

    cJSON_AddItemToObject(cJSON_Payload,"memUsed",cJSON_CreateNumber(10000));
    cJSON_AddItemToObject(cJSON_Payload,"memUsedPct",cJSON_CreateString("50"));
    cJSON_AddItemToObject(cJSON_Payload,"memUsed",cJSON_CreateNumber(18888));

    cJSON_AddItemToObject(root,"payload",cJSON_Payload);

    char* payload = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);

    char topic[200] = "";
    sprintf(topic, "sys/dev/ODQwODQ3MTgzNzA0Mjg5Mjgw/%s", "TEST0123456789");
    mqtt_pub(handle,topic,payload,0);
    free(payload);

    return 0;
}

int api_read_static_rsp(mqtt_handle_t* handle,char *msgid,int timestamp)
{
    cfg_pu_t* pu = (cfg_pu_t*)handle->param;
    api_t* api = (api_t*)pu->hanlder.api;

    //  UTIL_LOG_I(pu,"read static response...");

    cJSON* root = cJSON_CreateObject();
    cJSON_AddItemToObject(root,"msgid",cJSON_CreateString(msgid));
    cJSON_AddItemToObject(root,"method",cJSON_CreateString("info"));
    cJSON_AddItemToObject(root,"sn",cJSON_CreateString("TEST0123456789"));
    cJSON_AddItemToObject(root,"timestamp",cJSON_CreateNumber(timestamp));
    cJSON_AddItemToObject(root,"res",cJSON_CreateNumber(1));

    cJSON* cJSON_Payload = cJSON_CreateObject();
    cJSON_AddItemToObject(cJSON_Payload,"product",cJSON_CreateString("AWT200"));
    cJSON_AddItemToObject(cJSON_Payload,"productKey",cJSON_CreateString("ODQwODQ3MTgzNzA0Mjg5Mjgw"));
    cJSON_AddItemToObject(cJSON_Payload,"softcode",cJSON_CreateString("0000"));
    cJSON_AddItemToObject(cJSON_Payload,"softversion",cJSON_CreateString("1000"));
    cJSON_AddItemToObject(cJSON_Payload,"softbootload",cJSON_CreateString("100"));
    cJSON_AddItemToObject(cJSON_Payload,"softdevelopment",cJSON_CreateString("001"));
    cJSON_AddItemToObject(cJSON_Payload,"produceTime",cJSON_CreateString("20210101"));

    cJSON_AddItemToObject(cJSON_Payload,"imei",cJSON_CreateString("862810045876215"));
    cJSON_AddItemToObject(cJSON_Payload,"ccid",cJSON_CreateString("89860322745107594964"));

    cJSON_AddItemToObject(root,"payload",cJSON_Payload);

    char* payload = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);

    char topic[200] = "";
    sprintf(topic, "sys/dev/ODQwODQ3MTgzNzA0Mjg5Mjgw/%s", "TEST0123456789");
    mqtt_pub(handle,topic,payload,0);
    free(payload);

    return 0;
}

int api_login(mqtt_handle_t* handle)
{
    cfg_pu_t* pu = (cfg_pu_t*)handle->param;
    api_t* api = (api_t*)pu->hanlder.api;

    //  UTIL_LOG_I(pu,"ANetIOT LoginIn EIOT...");

    cJSON* root = cJSON_CreateObject();

    cJSON_AddItemToObject(root,"msgid",cJSON_CreateNumber(msgid++)); // FIXME: string
    cJSON_AddItemToObject(root,"method",cJSON_CreateString("login"));
    cJSON_AddItemToObject(root,"sn",cJSON_CreateString("TEST0123456789"));
    cJSON_AddItemToObject(root,"timestamp",cJSON_CreateNumber(util_hw_get_systs()));

    cJSON* cJSON_Payload = cJSON_CreateObject();
    cJSON_AddItemToObject(cJSON_Payload,"compress",cJSON_CreateString("0"));
    cJSON_AddItemToObject(cJSON_Payload,"compressAlgo",cJSON_CreateString("LZO")); // ZLIB
    cJSON_AddItemToObject(cJSON_Payload,"encrypt",cJSON_CreateString("0"));
    cJSON_AddItemToObject(cJSON_Payload,"devname",cJSON_CreateString("AF-HK100_4G"));
    cJSON_AddItemToObject(cJSON_Payload,"softcode",cJSON_CreateString("1111"));
    cJSON_AddItemToObject(cJSON_Payload,"softversion",cJSON_CreateString("1001"));
    cJSON_AddItemToObject(cJSON_Payload,"network",cJSON_CreateNumber(0));
    cJSON_AddItemToObject(cJSON_Payload,"imei",cJSON_CreateString("001122334455667"));
    cJSON_AddItemToObject(cJSON_Payload,"ccid",cJSON_CreateString("00112233445566778899"));

    cJSON_AddItemToObject(root,"payload",cJSON_Payload);

    char* payload = cJSON_PrintUnformatted(root);

    cJSON_Delete(root);

    char topic[200] = "";
    sprintf(topic, "sys/dev/ODQwODQ3MTgzNzA0Mjg5Mjgw/%s", "TEST0123456789");
    mqtt_pub(handle,topic,payload,0);
    free(payload);

    return 0;
}
